package edu.boun.g4.coursity.controller;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.Comment;
import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.Initiative;
import edu.boun.g4.coursity.domain.Institution;
import edu.boun.g4.coursity.domain.Instructor;
import edu.boun.g4.coursity.service.CommentService;
import edu.boun.g4.coursity.service.CourseService;
import edu.boun.g4.coursity.service.InitiativeService;
import edu.boun.g4.coursity.service.InstitutionService;
import edu.boun.g4.coursity.service.InstructorService;
import edu.boun.g4.coursity.service.LanguageService;
import edu.boun.g4.coursity.service.SemanticTagService;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author bulutkorkmaz
 * 
 *        Parses course data from other sources
 */
@Controller
@RequestMapping("parse")
public class ParseController {

	@Resource(name = "courseService")
	private CourseService courseService;

	@Resource(name = "initiativeService")
	private InitiativeService initiativeService;

	@Resource(name = "institutionService")
	private InstitutionService institutionService;

	@Resource(name = "instructorService")
	private InstructorService instructorService;

	@Resource(name = "languageService")
	private LanguageService languageService;

	@Resource(name = "commentService")
	private CommentService commentService;

	@Resource(name = "semanticTagService")
	private SemanticTagService semanticTagService;

	private String moocSource = "http://www.mooc-list.com/";
	
	@RequestMapping(method = RequestMethod.GET, value = "/institution")
	public @ResponseBody
	Response parseInstitution() {
		Document doc = null;
		try {
			doc = Jsoup.connect(moocSource)
					  .data("query", "Java")
					  .timeout(30000)
					  .get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Response response = new Response();
		Elements institutions = doc.select("#block-menu-menu-universities-entities a");
		 Set<String> s=new TreeSet<String>();  
		for (Element inst : institutions) {
			s.add(inst.attr("title"));

			Institution institution = new Institution();
			institution.setInstitutionName(inst.attr("title"));
			institution.setInfo(inst.attr("href"));
			institutionService.add(institution);
	       
	        }

		response.setBody(s);
		return response;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/instructors")
	public @ResponseBody
	Response parseInstructors() {
		 Set<String> s=new TreeSet<String>();  
		 Response response = new Response();
		for(int i=0;i<42;i++){ 
			Document doc = null;
			try {
				doc = Jsoup.connect(moocSource+"/instructors")
						  .data("page",Integer.toString(i))
						  .timeout(30000)
						  .get();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			
			Elements instructors = doc.select("#block-system-main .field-content a");
		
			for (Element inst : instructors) {
				s.add(inst.text());
	
				Instructor instructor = new Instructor();
				instructor.setInstructorName(inst.text());
				instructor.setInfo(inst.attr("href"));
				instructorService.add(instructor);
		       
		        }

		}
		response.setBody(s);
		return response;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/course")
	public @ResponseBody
	Response parseCourse() {
		 Set<String> s=new TreeSet<String>();  
		 Response response = new Response();
		for(int i=0;i<5;i++){
			Document doc = null;
			try {
				doc = Jsoup.connect(moocSource)
						  .data("page",Integer.toString(i))
						  .timeout(30000)
						  .get();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			
			Elements links = doc.select("#block-views-latest-submitted-courses-block .field-content a");
		
			for (Element link : links) {
				this.parseSingleCourse(link.attr("href"));
		    }

		}
		response.setBody(s);
		return response;
	}
	
	void parseSingleCourse(String link)
	{
		Document doc = null;
		try {
			doc = Jsoup.connect(moocSource+link)
					  .data("query", "Java")
					  .timeout(30000)
					  .get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Response response = new Response();

		 Set<String> s=new TreeSet<String>();  
		 Course course = new Course();
		 
		 course.setInitiative( initiativeService.getByInfo(doc.select(".field-name-field-initiative a").attr("href")));
		 course.setInstitution( institutionService.getByInfo(doc.select(".field-name-field-university-entity a").attr("href")) );
		
	
		 //Set<Instructor> instructors = new HashSet<Instructor>();
		 for (Element inst : doc.select(".field-name-field-instructors a")) {

			//	instructors.add( instructorService.getByInfo(inst.attr("href")));
				course.setInstructor( instructorService.getByInfo(inst.attr("href") ));
				break;
		 }
		 
	
		/* Set<Category> categories = new HashSet<Category>();
		 for (Element inst : doc.select("#field-name-field-instructors a")) {

			 categories.add( categoryService.getByInfo(inst.attr("href")));
		 
		 }
		 course.setCategories(categories );
			*/
		 
		 course.setCourseDescription(doc.select(".field-type-text-with-summary").html());
		 
		 course.setStartingDate(doc.select(".field-name-field-start-date-text .field-items").text());
		 
		 
		 course.setLength(doc.select(".field-name-field-length .field-items").text());
		 course.setEstimatedEffort(doc.select(".field-name-field-estimated-effort .field-items").text());
		 course.setRecommendedBackground(doc.select(".field-name-field-recommended-background .field-items").text());
		 course.setCertificate(doc.select(".field-name-field-certificate .field-items").text());
		 course.setTextbook(doc.select(".field-name-field-textbook-materials .field-items").text());
		 course.setCountry(doc.select(".field-name-field-country .field-items").text());
		 
		 course.setLanguages(doc.select(".field-name-field-language .field-items").text());
		 course.setCourseName(doc.select("#page-title").text());
		
		 

		courseService.add(course);
		
		/* for (Element inst : doc.select(".field-name-field-instructors a")) {

			 Instructor existingInstructor = instructorService.getByInfo(inst.attr("href"));
			 Set<Course> courses = existingInstructor.getCourses();
			 courses.add(courseService.get(course.getCourseId()));
			 existingInstructor.setCourses(courses);
			 instructorService.edit(existingInstructor);

		 }*/



	
	
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/initiative")
	public @ResponseBody
	Response parseInitiative() {
		
		Document doc = null;
		try {
			doc = Jsoup.connect(moocSource)
					  .data("query", "Java")
					  .timeout(30000)
					  .get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Response response = new Response();
		Elements initiatives = doc.select("#block-menu-menu-initiatives--2 li a");
		 Set<String> s=new TreeSet<String>();  
		for (Element inst : initiatives) {
			s.add(inst.attr("title"));

			Initiative initiative = new Initiative();
			initiative.setInitiativeName(inst.text());
			initiative.setInfo(inst.attr("href"));
			initiativeService.add(initiative);
	       
	        }

		response.setBody(s);
		return response;
	}
	
}